#include "xmlparsertest.h"

/*Registry this TestSuite to theTestFactoryRegistry*/
CPPUNIT_TEST_SUITE_REGISTRATION (XMLParserTest);

void XMLParserTest::setUp() {
}

void XMLParserTest::testConstructor() { fprintf(stderr,">>> XMLParserTest::testConstructor()\n");
	XMLParser *p = NULL;
	try {
		p = new XMLParser("NO_FILE");	
		CPPUNIT_FAIL("An exception should be throwed!! No file can be opened");
	} catch (std::runtime_error &e) {
//		Pass
	}
	p = new XMLParser("../data/unittest.xml");	
	
	delete p;
}

void XMLParserTest::testParse() { fprintf(stderr,">>> XMLParserTest::testParse()\n");
	XMLParser *p = new XMLParser("../data/unittest.xml");
	p->parse();	
	delete p;
	
/*	p = new XMLParser("../data/traces/cputrace.0");	
	try {
	p->parse();	
	CPPUNIT_FAIL("An exception should be throwed!! No file can be opened");
	} catch (std::runtime_error &e) {
//		Pass
	}
	delete p;*/
}

void XMLParserTest::testGetParameter() { fprintf(stderr,">>> XMLParserTest::testGetParameter()\n");
	
	XMLParser *p = new XMLParser("../data/unittest.xml");
	p->parse();
	
	CPPUNIT_ASSERT(!strcmp(p->getParameter("Scenario", "name"),"scene1"));
	CPPUNIT_ASSERT_EQUAL(atoi( p->getParameter("Scenario", "numberOfExecutions")),2);
	CPPUNIT_ASSERT(atof(p->getParameter("Scenario", "seed"))==0.214);
	
	CPPUNIT_ASSERT_EQUAL(atoi( p->getParameter("Grid", "numberOfSites")),3);
	
	CPPUNIT_ASSERT_EQUAL(atoi( p->getParameter("Job", "numberOfTasks")),10);
	
	CPPUNIT_ASSERT(atof( p->getParameter("Task", "inputDataSize","task_1"))==500);
	CPPUNIT_ASSERT(atof( p->getParameter("Task", "inputDataSize", "task_2"))==600);
	CPPUNIT_ASSERT(atof( p->getParameter("Task", "inputDataSize", "task_3"))==700);
	
	CPPUNIT_ASSERT_EQUAL(atoi( p->getParameter("Site", "numberOfProcessors", "site1")),0);
	CPPUNIT_ASSERT_EQUAL(atoi( p->getParameter("Site", "numberOfProcessors", "site2")),3);
		
	CPPUNIT_ASSERT(!strcmp(p->getParameter("Processor", "name", "processor_1_site2"),"processor_1_site2"));
	CPPUNIT_ASSERT(atof( p->getParameter("Processor", "cpuspeed", "processor_1_site2"))==3);
	CPPUNIT_ASSERT(atof( p->getParameter("Processor", "cpuspeed", "processor_3_site3"))==2);
	

	CPPUNIT_ASSERT(!strcmp(p->getParameter("Link", "name", "interlink_site1_site2"),"interlink_site1_site2"));
	CPPUNIT_ASSERT(!strcmp(p->getParameter("Link", "lat_trace", "interlink_site1_site3"),"../data/traces/cputrace.0"));
	
	//Test Exception
	try {
		const char *parameter = p->getParameter("FakeNode", "numberOfProcessors", "site1");
		CPPUNIT_ASSERT_EQUAL(atoi( parameter ),0);
		CPPUNIT_FAIL("An exception should be throwed!!");
	}catch (std::runtime_error &e) { }
	
	try { 
		const char *parameter = p->getParameter("Site", "FakeAttribute", "site1");
		CPPUNIT_ASSERT_EQUAL(atoi( parameter ),0);
		CPPUNIT_FAIL("An exception should be throwed!!");
	}catch (std::runtime_error &e1) {}
	
	try {
		const char *parameter = p->getParameter("Site", "numberOfProcessors", "site45");
		CPPUNIT_ASSERT_EQUAL(atoi( parameter ),0);
		CPPUNIT_FAIL("An exception should be throwed!!");
	}catch (std::runtime_error &e2) {}

}

void XMLParserTest::tearDown() {
}
